package com.lzy.business.controller.agent;

import com.github.pagehelper.PageInfo;
import com.lzy.base.utils.UserUtil;
import com.lzy.business.controller.BaseController;
import com.lzy.business.facade.AgentProtocolFacade;
import com.lzy.business.request.AgentProtocolRequest;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.apis.ProductUserServiceApi;
import com.lzy.product.dtos.ProductUserDto;
import com.lzy.product.dtos.agent.AgentProtocolDto;
import com.lzy.product.dtos.agent.AgentProtocolInfoDto;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by WIN7 on 2018/4/13.
 */
@Controller
@RequestMapping("/admin/agent/protocol")
public class AgentProtocolController extends BaseController {

    @Autowired
    private AgentProtocolFacade agentProtocolFacade;
    @Autowired
    private ProductUserServiceApi productUserServiceApi;


    @RequestMapping(value = "", method = RequestMethod.GET)
    public String protocolList() {
        return "/agent/agreement/agree_list";
    }

    @RequestMapping(value = "/toAdd", method = RequestMethod.GET)
    public String protocolAdd() {
        return "/agent/agreement/agree_add";
    }

    @RequestMapping(value = "/toView", method = RequestMethod.GET)
    public String protocolView() {
        return "/agent/agreement/agree_view";
    }

    /**
     * 获取详情数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/getAgentProtocolInfo", method = RequestMethod.GET)
    @ResponseBody
    public Result getAgentProtocolInfo(@RequestParam(value = "id")Long id) {
        Result result = new Result();
        AgentProtocolInfoDto agentProtocolInfoDto = agentProtocolFacade.getAgentProtocolInfo(id);
        result.setResult(agentProtocolInfoDto);
        return result;
    }

    /**
     * 新增协议
     *
     * @param agentProtocolInfoDto
     * @return
     */
    @RequestMapping(value = "/createAgentProtocol", method = RequestMethod.POST)
    @ResponseBody
    public Result createAgentProtocol(AgentProtocolInfoDto agentProtocolInfoDto) {
        Boolean flag = agentProtocolFacade.createAagntProtocol(agentProtocolInfoDto);
        Ensure.that(flag).isTrue("07000002");
        return new Result();
    }

    /**
     * 协议列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/agentProtocolList", method = RequestMethod.GET)
    @ResponseBody
    public PageBean agentProtocolList(AgentProtocolRequest request) {
        Ensure.that(request).isNotNull("04000001");
        Pager pager = new Pager();
        pager.setPageSize(request.getPageSize());
        pager.setPageNum(request.getPageNum());
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated() || subject.isRemembered()) {
            boolean isAdmin = isAdmin();
            if(!isAdmin){
                Long userId = UserUtil.getCurrentUser().getId();
                ProductUserDto productUserDto = productUserServiceApi.findById(userId);
                if(null != productUserDto.getSystemId() && !"".equals(productUserDto.getSystemId())){
                    request.setSystemId(productUserDto.getSystemId());
                }else{
                    request.setSystemId(0L);
                }
            }
        }
        PageInfo<AgentProtocolDto> pageInfo = agentProtocolFacade.agentProtocolList(pager, DozerUtils.map(request, AgentProtocolDto.class));
        if (pageInfo != null) {
            return new PageBean<>(pageInfo.getTotal(), DozerUtils.maps(pageInfo.getList(), AgentProtocolRequest.class));
        }
        return null;
    }

    /**
     * 更改协议状态
     *
     * @param agentProtocolDto
     * @return
     */
    @RequestMapping(value = "/updateAgentProtocolStatus", method = RequestMethod.POST)
    @ResponseBody
    public Result updateAgentProtocolStatus(AgentProtocolDto agentProtocolDto) {
        Boolean flag = agentProtocolFacade.updateAgentProtocolStatus(agentProtocolDto);
        Ensure.that(flag).isTrue("07000002");
        return new Result();
    }

    /**
     * 更改协议
     *
     * @param agentProtocolInfoDto
     * @return
     */
    @RequestMapping(value = "/updateAgentProtocol", method = RequestMethod.POST)
    @ResponseBody
    public Result updateAgentProtocol(AgentProtocolInfoDto agentProtocolInfoDto) {
        Boolean flag = agentProtocolFacade.updateAgentProtocol(agentProtocolInfoDto);
        Ensure.that(flag).isTrue("07000002");
        return new Result();
    }

}
